﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OfficeOpenXml;
using System.Configuration;
using OfficeOpenXml.Style;
using System.Drawing;
using ARR.Entities;
using ARR.DataModel.Repositories;
using ARR.Entities.Gateways;

namespace ARR.Services.Domain.Exporters
{
    public static class ArticleMasterExport
    {
        public static void ExportArticleMaster(RangeReview rangeReview)
        {
            FileInfo newFile = ExportHelper.CreateExcelFile("ArticleMaster.xlsx");
            List<Article> articles = ExportHelper.GetArticlesToExport(rangeReview);

            //Categories and Subcategories Cache
            List<CategoryRow> categories = CategoriesRepository.Select();
            CategoryRow category;
            CategoryRow subcategory;
            
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Article Master");
                //Add the headers
                worksheet.Cells[1, 1].Value = "Code";
                worksheet.Cells[1, 2].Value = "Description";
                worksheet.Cells[1, 3].Value = "MC";
                worksheet.Cells[1, 4].Value = "Category";
                worksheet.Cells[1, 5].Value = "Subcategory";
                worksheet.Cells[1, 6].Value = "Brand";
                worksheet.Cells[1, 7].Value = "Manufacturer";
                worksheet.Cells[1, 8].Value = "Status";

                //Freeze headers
                worksheet.View.FreezePanes(2, 1);
                

                Article article;
                int row = 0;
                for (int i = 0; i < articles.Count; i++)
                {
                    row = i + 2;
                    article = articles[i];

                    subcategory = categories.Where(cat => cat.Id == article.CategoryId).SingleOrDefault();
                    category = categories.Where(cat => cat.Id == subcategory.ParentId).SingleOrDefault();

                    worksheet.Cells[row, 1].Value = article.Code;
                    worksheet.Cells[row, 2].Value = article.Name;
                    worksheet.Cells[row, 3].Value = subcategory.Code;
                    worksheet.Cells[row, 4].Value = subcategory.Name;
                    worksheet.Cells[row, 5].Value = category.Name;
                    worksheet.Cells[row, 6].Value = article.Brand;
                    worksheet.Cells[row, 7].Value = article.Manufacturer;
                    worksheet.Cells[row, 8].Value = article.IsDelisted ? "De-listed" : "Active";
                }
                              

                //Ok now format the values;
                using (var range = worksheet.Cells[1, 1, 1, 8])
                {
                    range.Style.Font.Bold = true;
                    range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
                    range.Style.Font.Color.SetColor(Color.White);
                }
                
                //Increase the width of column one as these strings will be too wide to display
                worksheet.Column(1).Width = 15;
                worksheet.Column(2).Width = 30;

                //Create an autofilter for the range
                worksheet.Cells["A1:H1"].AutoFilter = true;
                                
                // save our new workbook and we are done!
                package.Save();
            }
        }
    }
}
